﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LP580.CG;

public partial class User_UC2GiftClassOperate : System.Web.UI.Page
{
    private int id = Utils.RQ_int("id");
    private string act = Utils.RQ_str("act");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindData();
        bindContrl();
    }

    private void bindData()
    {
        if (Request.UrlReferrer != null)
            hfdFromUrl.Value = Request.UrlReferrer.ToString();

        int uid = Master.ua.UserID;
        act = act.ToLower();
        switch (act)
        {
            case "edit":
                clsGiftProductType cls = new clsGiftProductType(id);
                if (cls.UserID != uid)
                    Response.End();
                ltTitle.Text = "修改分类";
                tbxName.Text = cls.GiftProductTypeName;
                tbxOrder.Text = cls.GiftProductTypeOrder.ToString();
                break;
            case"del":
                DealDelete();
                break;
            case "goup":
                dealGoOrder(true);
                break;
            case "godown":
                dealGoOrder(false);
                break;
            default:
                tbxOrder.Text = clsGiftProductType.GetNextOrderID(uid).ToString();
                break;
        }
    }

    private void bindContrl()
    {

    }

    private void DealDelete()
    {
        int uid = Master.ua.UserID;
        List<clsGiftProductType> ls=getList();
        foreach (clsGiftProductType pt in ls)
        {
            if (pt.UserID == uid)
                clsGiftProductType.Delete(pt);
        }
        string goUrl = "";
        if (Request.UrlReferrer != null)
            goUrl = Request.UrlReferrer.ToString();
        if (goUrl == "")
        {
            goUrl = "UC2GiftClass.aspx";
        }
        Response.Redirect(goUrl, true);
    }

    private void dealGoOrder(bool up)
    {
        int uid = Master.ua.UserID;
        clsGiftProductType cls = new clsGiftProductType(id);
        if (cls.UserID == uid)
        {
            if (up)
                cls.GoMoveOrderUp();
            else
                cls.GoMoveOrderDown();
        }
        string goUrl = "";
        if (Request.UrlReferrer != null)
            goUrl = Request.UrlReferrer.ToString();
        if (goUrl == "")
        {
            goUrl = "UC2GiftClass.aspx";
        }
        Response.Redirect(goUrl, true);
    }

    private List<clsGiftProductType> getList()
    {
        string ids = Utils.RQ_str("ids");
        List<clsGiftProductType> ls = new List<clsGiftProductType>();
        if (ids != "")
        {
            string[] arrID = ids.Split(',');
            int arrLen = arrID.Length;
            for (int i = 0; i < arrLen; i++)
            {
                int id = 0;
                if (!int.TryParse(arrID[i].Trim(), out id))
                    id = 0;
                if (id > 0)
                {
                    clsGiftProductType s = new clsGiftProductType(id);
                    if (s.GiftProductTypeID > 0)
                        ls.Add(s);
                }
            }
        }
        return ls;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int uid = Master.ua.UserID;

        string name = tbxName.Text.Trim();
        if (name == "")
        {
            Utils.WrAlertGoBack("标注*号为必填不可为空！");
        }

        int iorder = 0;
        if (!int.TryParse(tbxOrder.Text, out iorder))
            iorder = 0;
        
        clsGiftProductType cls=new clsGiftProductType();

        if (act.ToLower() == "edit")
        {
            cls = new clsGiftProductType(id);
            if (cls.UserID != uid)
                Response.End();
            cls.GiftProductTypeName = name;
            cls.GiftProductTypeOrder = iorder;
            clsGiftProductType.Update(cls);
        }
        else
        {
            cls.UserID = uid;
            cls.GiftProductTypeName = name;
            cls.GiftProductTypeOrder = iorder;
            clsGiftProductType.Insert(cls);
        }

        string goUrl = hfdFromUrl.Value;
        if (goUrl == "")
        {
            goUrl = "UC2GiftClass.aspx";
        }
        Utils.WrAlertGo("操作成功！", goUrl);
    }
}